﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace VSTSDemo
{
    public class LongonInfo
    {
        private string _Password;
        public string Password { 
            get { return _Password; }
            private set {
                string errorMessage = string.Empty;
                if (!IsPasswordValid(value,out errorMessage))                     
                    throw new InvalidOperationException("Password format is not correct!");
                _Password = value;
            }
        }

        private string _ID;
        public string ID { get; set; }

        public LongonInfo()
        { }

        public LongonInfo(string password, string id)
        {
            Password = password;
            ID = id;
        }

        public bool IsLongOnValid()
        { 
            bool result= false ;
            return result;
        }

        public bool IsPasswordValid(string value, out string errorMessage)
        {
            const string passwordSizeRegex = "(?=^.{6,255}$)";
            const string uppercaseRegex = "(?=.*[A-Z])";
            const string lowercaseRegex = "(?=.*[a-z])";
            const string punctuationRegex = @"(?=.*\d)";
            const string upperlowernumericRegex = "(?=.*[^A-Za-z0-9])";

            bool isValid;
            Regex regex = new Regex(
               passwordSizeRegex +
               "(" + punctuationRegex + uppercaseRegex + lowercaseRegex +
                  "|" + punctuationRegex + upperlowernumericRegex + lowercaseRegex +
                  "|" + upperlowernumericRegex + uppercaseRegex + lowercaseRegex +
                  "|" + punctuationRegex + uppercaseRegex + upperlowernumericRegex +
               ")^.*");

            if (value == null || value.Trim() == string.Empty)
            {
                isValid = false;
                errorMessage = "Password may not be null or blank.";
            }
            else
            {
                if (regex.Match(value).Success)
                {
                    isValid = true;
                    errorMessage = "";
                }
                else
                {
                    isValid = false;
                    errorMessage = "Password does not meet the complexity requirements.";
                }
            }
            return isValid;

        }

        public void ChangPassword(string oldPassword,string newPassword)
        {
            if (oldPassword == Password)
            {
                Password = newPassword;
            }
            else
            {
                throw new InvalidOperationException("the old password is not correct!");
            }
        }
    }
}
